package com.ken.algorithms.stack;

import java.util.Scanner;
import java.util.Stack;

/**
 * <ul>
 * <li>Title: BrowserStep</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author swc
 * @date 2021-09-22 14:16
 */
public class BrowserStep {

    public static void main(String[] args) {

        Scanner scanner =new Scanner(System.in);
        Stack<String> stack =new Stack<String>();
        Stack<String> assistStack =new Stack<String>();

        for (int i = 0; i < 5; i++) {
            stack.push(scanner.nextLine());
        }
        // 百度-》抖音 -》 某抖主主页 =》某抖主的视频002

        System.out.println("请输入上一步(pref)，或下一步(next),退出请输入quit");
        String flag;
        while (scanner.hasNext()){
             flag =scanner.nextLine();
            if("quit".equals(flag)){
                break;
            }else if("pref".equals(flag)){
                goPref(stack,assistStack);
            }else if("next".equals(flag)){
                goNext(stack,assistStack);
            }
        }

    }

    private static void goNext(Stack<String> stack, Stack<String> assistStack) {
        if(assistStack==null || assistStack.isEmpty()){
            System.out.println("*********已经是最后了*********");
        }else{
            String content = assistStack.pop();
            System.out.println("下一步内容是:"+content);
            stack.push(content);
        }
    }

    private static void goPref(Stack<String> stack, Stack<String> assistStack) {
        if(stack==null || stack.isEmpty()){
            System.out.println("*********已经是最前了*********");
        }else{
            String content =  stack.pop();//栈顶是当前页 要去掉 并流入辅助栈
            if(stack.isEmpty()){
                System.out.println("*********已经是最前了*********");
                return;
            }
            System.out.println("上一步内容是:"+stack.peek());
            assistStack.push(content);
        }
    }

}
